
//工具类
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:crypto/crypto.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';

class DxmUtils{

   static  Pattern pathPrefixA =  RegExp("(?<!:)[/]+");
   static  Pattern pathPrefixB = RegExp("\\\\");
   static  String cleanUrl(String? url){
     String result = (url ?? "").replaceAll(pathPrefixB, "/");
     return result.replaceAll(pathPrefixA,"/");
   }
   static String md5Str(String? str){
      return md5.convert(utf8.encode(toStr(str))).toString();
   }
   static String toStr(String? str){
     return (str ?? "").trim();
   }

   static List<String> trimListStr(List<String?>? texts){
     List<String?> ktexts = texts ?? [];
     List<String> textList = [];
     for(String? txt in ktexts){
       if(toStr(txt).isNotEmpty){
         textList.add(toStr(txt));
       }
     }
     return textList;
   }
  static List<List<T>> pageDatas<T>(List<T>? datas,int pageSize){
    if(datas == null || datas.isEmpty){
        return [];
    }
    List<List<T>> pageDatas = [];
    if(pageSize <= 0){
      pageSize = 50;
    }
    int size = datas.length;
    int totalPage = ((size+pageSize-1)/pageSize).floor();
    for(int page = 1;page<=totalPage;page++){
      List<T> pageList = [];
      int startOffset = (page-1)*pageSize;
      int endOffset = startOffset+pageSize;
      if(endOffset >= size ){
        endOffset = size;
      }
      for(int index = startOffset;index<endOffset;index++){
        pageList.add(datas[index]);
      }
      if(pageList.isNotEmpty ){
        pageDatas.add(pageList);
      }
    }
    return pageDatas;
  }

  static toastMsg(String msg){
    Fluttertoast.showToast(
        msg: msg,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIosWeb:1,
        backgroundColor: Colors.black54,
        textColor: Colors.white,
        fontSize: 13.0
    );
  }


}